package com.ktjy.springredis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.springredis.entity.Role;
import com.ktjy.springredis.entity.User;
import com.ktjy.springredis.service.IRoleService;
import com.ktjy.springredis.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @return 13141
 * 2025/7/16
 * 08:43
 */
@Controller
// @RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private IRoleService roleService;

    @RequestMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    @RequestMapping(value = "/dologin")
    public String dologin(String usrName, String usrPassword, HttpServletRequest request) {
        User user = userService.login(usrName, usrPassword);
        if (user != null) {
            request.getSession().setAttribute("loginUser", user);
            return "main";
        } else {
            request.setAttribute("message", "登录失败，用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login";
    }

    @RequestMapping(value = "/user/list")
    public String list(Model model,
                       String usrName,
                       @RequestParam(required = false, defaultValue = "0") Long roleId,
                       @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("usrName", usrName);
        hashMap.put("roleId", roleId);
        hashMap.put("pageIndex", pageIndex);
        hashMap.put("pageSize", 3);
        IPage<User> userIPage = userService.getUserByPage(hashMap);
        model.addAttribute("userPager", userIPage);
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        // 查询所有角色
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        return "/user/list";
    }

    // 跳转新增页面
    @GetMapping("/user/add")
    public String toAdd(Model model) {
        // 查询所有角色
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        return "user/add";
    }

    // 保存用户信息到数据库
    @PostMapping("/user/add")
    public String add(User user) {
        int result = userService.addUser(user);
        if (result > 0) {
            return "redirect:/user/list";
        } else {
            return "redirect:/user/add";
        }
    }

    // 跳转到修改页面
    @GetMapping("/user/edit")
    public String toEdit(Long usrId, Model model) {
        User user = userService.getUser(usrId);
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("user", user);
        model.addAttribute("roles", roles);
        return "/user/edit";
    }

    // 更新用户信息到数据库
    @PostMapping("/user/edit")
    public String edit(User user) {
        int result = userService.updateUser(user);
        if (result > 0) {
            return "redirect:/user/list";
        } else {
            return "redirect:/user/edit";
        }
    }

    // 用户删除
    @RequestMapping("/user/del")
    @ResponseBody
    public Map del(Long usrId) {
        int result = userService.deleteUser(usrId);
        Map map = new HashMap();
        if (result > 0) {
            map.put("delResult", true);
        } else {
            map.put("delResult", false);
        }
        return map;
    }

    // 查询所有角色，返回 JSON 格式数据
    @RequestMapping("/role/json")
    @ResponseBody
    public List<Role> findAllRoles() {
        List<Role> roles = roleService.findAllRoles();
        return roles;
    }
}
